Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts:[],    // 购物车列表
    hasList:false,   // 列表是否有数据
    totalPrice:0,   // 总价，初始为0
    selectAllStatus:true // 全选状态，默认全选
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // let carts=wx.getStorageSync("carts");
    this.setData({
         hasList: true,  // 有数据
         carts:[
         {id:1,title:'广西六堡茶',image:'https://bkimg.cdn.bcebos.com/pic/279759ee3d6d55fb266cd48c67224f4a21a4ddf6?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5',num:3,price:100,selected:true},
         {id:2,title:'紫砂茶壶单壶',image:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2FObS4en8VW28zuyq7-vCKqg.jpg_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1674390795&t=56eb0dd11ec02551a0453f4e207d05de',num:1,price:200,selected:true}
         ]
        });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getTotalPrice(){
    let carts = this.data.carts;
    let totalPrice = 0;
    let totalNum = 0;
    for(let i =0;i<carts.length;i++){
      // 被选中计算价格
      if(carts[i].selected){
        totalPrice += carts[i].num *carts[i].price;
        totalNum += carts[i].num
      }
    }
    this.setData({
      carts:carts,
      totalPrice:totalPrice.toFixed(2),
      totalNum:totalNum
    })
  },
  selectList(e){
    //获取index
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts; 
    // 获取商品是否非选中
    const selected = carts[index].selected; 
    // 改变状态
    carts[index].selected =!selected;
    this.setData({
        carts: carts
    });
    // 获取总价
    this.getTotalPrice();  
  },
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let carts = this.data.carts;

    for(let i = 0;i<carts.length;i++){
      carts[i].selected  = selectAllStatus;
    }
    this.setData({
      selectAllStatus:selectAllStatus,
      carts:carts
    });
    this.getTotalPrice();
  },
  //商品数量的变化
  addCount(e){
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].num;
    num +=1;
    carts[index].num = num;
    this.setData({
      carts:carts
    })
    this.getTotalPrice();
  },
  reduceCount(e){
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].num;
    if(num <=1){
      return false
    }
    num = num -1;
    carts[index].num = num;
    this.setData({
      carts:carts
    })
    this.getTotalPrice();
  },
  //删除商品
  deleteList(e){
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    carts.splice(index,1);
    this.setData({
      carts:carts
    })
    if(!carts.length){
      this.setData({
        hasList:false
      })
    }else{
      this.getTotalPrice();
    }
  },
  //支付
  toBuy:function(){
    wx.navigateTo({
      url: '../payment/payment',
    })
  }
})